﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer timer;
        bool isDragging = false;

        public MainWindow()
        {
            InitializeComponent();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(200);
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (!isDragging)
            {
                sliderPosition.Value = MediaEl.Position.TotalSeconds;
            }
        }

        private void MediaEL_MediaOpen(object sender, RoutedEventArgs e)
        {
            if (MediaEl.NaturalDuration.HasTimeSpan)
            {
                TimeSpan ts = MediaEl.NaturalDuration.TimeSpan;
                sliderPosition.Maximum = ts.TotalSeconds;
                sliderPosition.SmallChange = 1;
                sliderPosition.LargeChange = Math.Min(10, ts.Seconds / 10);
            }
            timer.Start();
        }

        private void sliderPosition_DragStarted(object sender, DragStartedEventArgs e)
        {
            isDragging = true;
        }

        private void sliderPosition_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            isDragging = false;
            MediaEl.Position = TimeSpan.FromSeconds(sliderPosition.Value);
        }
    }
}
